/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utn.tsb.tej.front_end.Maps;

import com.trolltech.qt.core.QPoint;
import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QGraphicsItem;
import com.trolltech.qt.gui.QGraphicsPixmapItem;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QGraphicsTextItem;
import com.trolltech.qt.gui.QGraphicsView;
import com.trolltech.qt.gui.QImage;
import com.trolltech.qt.gui.QMouseEvent;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QWidget;
import utn.tsb.tej.world_logic.Color;
import utn.tsb.tej.world_logic.Continent;
import utn.tsb.tej.world_logic.Country;
import utn.tsb.tej.world_logic.World;
import csm.uttils.generics.Roster;


/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Map extends QGraphicsView{

    private boolean countrieSelected;
    private char osBar;
    private QImage hardMap;
    private QGraphicsScene scene;

    private Arrow bArrow2;

    private QGraphicsTextItem textToShow;

    public Map (QWidget parent)
    {
        super(parent);

        this.setFixedSize(805, 596);

        //File Separator
        osBar = java.io.File.separatorChar;

        
        countrieSelected = false;

        //Set the Hard Map Image
        try{
//        hardMap = new QImage(getClass().getResource(osBar + "images"+
//                osBar + "HARD_MAP_V0.1.gif").getPath());
             hardMap = new QImage(getClass().getResource("/images/HARD_MAP_V0.1.gif").getPath());
        }
        catch (NullPointerException e)
        {
            System.out.println("[E]: Hardmap image not found!");
        }

        scene = new QGraphicsScene(this);


        //background
        try{
//        QPixmap map = new QPixmap(getClass().getResource(osBar + "images"+
//                osBar + "TEJ_MAP.png").getPath());
            QPixmap map = new QPixmap(getClass().getResource("/images/TEJ_MAP.png").getPath());
        QGraphicsPixmapItem background = new QGraphicsPixmapItem(map);
        background.setPos(0, 0);
        background.setZValue(Integer.MIN_VALUE);

        scene.addItem(background);
        }
        catch (NullPointerException e)
        {
            System.out.println("[E]: Map image not found!");
        }

        
        textToShow = scene.addText("T.E.J. - Under Development Version");
        //scene.addPixmap(map);

        this.setScene(scene);
    }

    public void mousePressEvent(QMouseEvent event)
    {
        
        if (bArrow2 != null) this.scene().removeItem(bArrow2);
        else
            bArrow2 = new Arrow();
        
        countrieSelected = true;


        this.scene().removeItem(textToShow);

        QColor color = new QColor(hardMap.pixel(event.pos()));
        utn.tsb.tej.world_logic.WorldLogicFacade.world =
                (World)(new csm.uttils.Archive(getClass().getResource("/tjFiles/World.wtj").getPath()).readObject());
        Country country = utn.tsb.tej.world_logic.WorldLogicFacade.getCountryByColor(color.name());
        textToShow = this.scene().addText( "Color: " +
                color.name() + " Country: " + country.getName() +"\nX: " + event.pos().x() +
                "\t Y: " + event.pos().y() + "\ncX: " + country.getxPosition() +
                "\t cY: " + country.getyPosition());

        bArrow2.setPos(country.getxPosition(), country.getyPosition());
        this.scene().addItem(bArrow2);

        this.scene().update();

    }

    public void drawWorld(World arg)
    {
        scene.clear();
        Roster<Continent> continents = arg.getContinents();
        while (continents.hasNext())
        {
            Roster<Country> countries = continents.next().getCountries();
            while (countries.hasNext())
            {
                Country country = countries.next();
                Chip chip = new Chip(country.getColor(),
                        country.getArmies(),
                        country.getMissile(),
                        country.getxPosition(),
                        country.getyPosition());
                addChip(chip);
            }
        }
    }

    private void addChip(Chip arg)
    {
        scene.addItem(arg);
    }

}
